﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KhaoSatSinhVienTotNghiep.Model;
namespace KhaoSatSinhVienTotNghiep.Model
{
  public partial  class SinhVien
    {
     private static KhaoSatSVTNDataContext context = new KhaoSatSVTNDataContext();

      public static List<SinhVien> GetAll()
      {
          return context.SinhViens.ToList();
      }

      public static bool CheckExist(string massv)
      {
          var result = from sv in context.SinhViens
                       where sv.MSSV==massv
                       select sv;
          if (result.Count() != 0)
              return true;
          return false;
      }

     

      public static SinhVien GetSinhVienByMSSV(string massv)
      {
          return context.SinhViens.Where(sv => sv.MSSV == massv).FirstOrDefault();
      }

      public static bool AddNew_Update(SinhVien sv)
      {
          if (CheckExist(sv.MSSV) == false)
          {
              context.SinhViens.InsertOnSubmit(sv);
              context.SubmitChanges();
              return true;
          }
          else
          {
              SinhVien sinhvien = GetSinhVienByMSSV(sv.MSSV);
              sinhvien.HoTen = sv.HoTen;
              sinhvien.NgaySinh = sv.NgaySinh;
              sinhvien.GioiTinh = sv.GioiTinh;
              sinhvien.DanToc = sv.DanToc;
              sinhvien.DiaChi1 = sv.DiaChi1;
              sinhvien.DiaChi2 = sv.DiaChi2;
              sinhvien.DienThoai = sv.DienThoai;
              sinhvien.Email = sv.Email;
              sinhvien.Lop = 1;
              string[] str = sv.NgaySinh.ToShortDateString().Split('/');
              string password = "";
              for (int i = 0; i < str.Count(); i++)
              {
                  password += str[i];
              }
              sinhvien.Password = password;
              context.SubmitChanges();
              return true;
          }
      }
    }

}
